import {fetchCateListAPI} from "@/api/categoryApi";

const actions = {
    /* 获取分类列表的action */
    async fetchCateList(context) {
        const {message: cateList} = await fetchCateListAPI()
        context.commit("updateCateList", cateList)
    }
}
const mutations = {
    /* 更新分类列表的mutation */
    updateCateList(state, cateList) {
        state.cateList = cateList
    },
    /* 修改一级分类的mutation */
    changeCateId(state, id) {
        state.cat_id = id
    },
    /* 修改scrollTop的mutation(scroll-view的滚动位置) */
    changeScrollTop(state, top) {
        state.scrollTop = top
    }
}
const state = () => {
    return {
        // 当前一级分类的iD
        cat_id: 1,
        // 所有的分类列表
        cateList: [],
        // scroll-view的滚动位置
        scrollTop: 0,
    }
}
const getters = {
    // 当前一级分类的子分类
    cateListChildren(state) {
        return state.cateList.find(item => item.cat_id === state.cat_id)?.children || []
    },

}
export default {
    namespaced: true,
    actions,
    mutations,
    state,
    getters
}
